<template>
  <el-dialog
    title="增加"
    :visible.sync="showDialog"
    width="50%"
    @close="btnCancel"
  >
    <!-- 表单 -->
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:70%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:70%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:70%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:70%" placeholder="请选择">
          <el-option v-for="item in employeesConstant.hireType" :key="item.id" :value="item.id" :label="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:70%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:70%" placeholder="请选择部门" @focus="getDepartments" />
        <el-tree v-if="showTree" v-loading="loading" :data="treeData" :props="{ label:'name' }" @node-click="selectNode" />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="formData.correctionTime" style="width:70%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- 底部插槽 -->
    <el-row slot="footer" type="flex" justify="center" align="middle">
      <el-button size="small" @click="showDialog=false">取消</el-button>
      <el-button size="small" type="primary" @click="btnOk">确定</el-button>
    </el-row>
  </el-dialog>
</template>
<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import employeesConstant from '@/constant/employees'
import { addEmployee } from '@/api/employees'
const defaultItem = {
  username: '',
  mobile: '',
  formOfEmployment: '',
  workNumber: '',
  departmentName: '',
  timeOfEntry: '',
  correctionTime: ''
}
export default {
  name: 'AddEmployee',
  data() {
    return {
      showDialog: false, // 弹框控制
      employeesConstant,
      formData: { ...defaultItem },
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      treeData: [], // 定义数组接收树形数据
      showTree: false, // 是否显示部门
      loading: false // 控制树的显示或者隐藏进度条
    }
  },
  methods: {
    // 确认事件
    async btnOk() {
      try {
        // 1. 数据校验
        this.$refs.form.validate()
        // 2. 发送请求
        await addEmployee(this.formData)
        // 3. 关闭弹框、重新拉取列表、重置表单
        this.showDialog = false
        // this.$parent  获取到父组件实例对象
        this.$parent.getEmployeesList()
      } catch (error) {
        console.log(error)
      }
    },
    // 取消事件
    btnCancel() {
      this.formData = { ...defaultItem }
      this.$refs.form.resetFields()
    },
    // 获取部门数据
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
    },
    // 树被点击了
    selectNode(val) {
      this.formData.departmentName = val.name
      this.showTree = false
    }
  }
}
</script>
<style  lang='scss' scoped>
</style>
